import util from "../../common/js/util.js";
var app = getApp();

Component({
  mixins: [],
  data: {
    showBottom: false,
    items: [
      { name: '1', value: '否', checked: true },
      { name: '2', value: '是' }
    ],
    labelList: [
      { text: "材料名称", name: "MATERIAL_NAME" },
      { text: "规格", name: "MODEL" },
      { text: "类别", name: "CATEGORY_NAME" },
      { text: "单位", name: "METERING_NAME" },
      { text: "中标数量", name: "NUM" },
      { text: "中标单价", name: "BID_PRICE" },   
      { text: "分析数量", name: "NUM1" },
      { text: "项目部预估数量", name: "NUM2" },
      { text: "分析单价", name: "UNIT_PRICE" },
      { text: "已签订采购数量", name: "purchaseOrderNum" },
      { text: "已入库", name: "entryWarehouseNum" },
      { text: "当前库存", name: "purchaseStockNum" },
    ],
    dataList:[],
    m_item: null,//品名列表 item
    radio:"0",//0否 1是 ,
  },
  props: {
    item:Object,
    type:"采购",//  采购 小i  合同 小i,这两种表现不同
  },
  didMount() { },
  didUpdate() { },
  didUnmount() { },
  methods: {
    switch1Change(e){
      let checked=e.detail.value
      let radio="0"
      if(checked){
        radio="1";
      }
      this.setData({
        radio
      })
      this.masterTableList();
    },
    //采购小i数据读取
    masterTableList() {
      if(util.isEmpty(this.data.m_item.PROJECT_ID)){
        this.$message.error("PROJECT_ID不能为空")
        return false;
      }
      let list = {
        PROJECT_ID: this.data.m_item.PROJECT_ID,
      }
      /**
       * radio
       * 0 读取 同材料 同规格   MATERIAL_ID: this.m_item.MATERIAL_ID,
       * 1 读取 同材料 不同规格 MATERIAL_NAME: this.m_item.MATERIAL_NAME,
       */
      if (this.data.radio == "1") {
        if(util.isEmpty(this.data.m_item.MATERIAL_NAME)){
          util.message("MATERIAL_NAME不能为空");
          return false;
        }
        list.MATERIAL_NAME = this.data.m_item.MATERIAL_NAME;
      } else {
        if(util.isEmpty(this.data.m_item.MATERIAL_ID)){
          util.message("MATERIAL_ID不能为空");
          return false;
        }
        list.MATERIAL_ID = this.data.m_item.MATERIAL_ID;
      }
      app.$post("/YhERP/datatool/CostControlMasterTable", list).then(res => {
        if (res.is == 1) {
          if (JSON.stringify(res.data) == "[]" && util.isNotEmpty(res.data) || (!util.isNotEmpty(res.data[0].MATERIAL_NAME) &&
            !util.isNotEmpty(res.data[0].MODEL) &&
            !util.isNotEmpty(res.data[0].METERING_NAME) &&
            !util.isNotEmpty(res.data[0].BID_PRICE) &&
            !util.isNotEmpty(res.data[0].UNIT_PRICE) &&
            !util.isNotEmpty(res.data[0].NUM) &&
            !util.isNotEmpty(res.data[0].entryWarehouseNum) &&
            !util.isNotEmpty(res.data[0].purchaseStockNum))) {
            this.setData({
              dataList:[]
            })
          } else {
            this.setData({
              dataList:res.data
            })
            this.table.initData(res.data)
          }
        } else if (res.is == 0) {
          this.setData({
              dataList:[]
            })
        }
      })
    },
    // 点击遮罩层
    maskClickHandler(e) {
      if (e.target.dataset.role === 'mask') {
        this.close()
      }
    },
    close(){
      this.setData({
        showBottom:false
      })
    },    
    openFn(item){
     // console.log(item);
     this.setData({
       m_item:item
     })
      this.onPopupOpen();
      if(this.props.type=="采购"){
        this.masterTableList()
      }else{

      }
    },
    table(ref){
      this.table=ref;
    },
    onPopupClose() {
      this.setData({
        showBottom: false
      });
    },
    onPopupOpen() {
      this.setData({
        showBottom: true
      });
    },
  },
});
